package sg.edu.nus.comp.icreate.ivletalk.forum;

import java.util.List;

import sg.edu.nus.comp.icreate.ivletalk.R;
import sg.edu.nus.comp.icreate.ivletalk.module.ElementType;
import sg.edu.nus.comp.icreate.ivletalk.module.MainActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ThreadActivity extends ForumBaseActivity {
	public static final String EXTRA_NAME_PARENT_THREAD_ID_TITLE = "sg.edu.nus.comp.icreate.ivletalk.extra.parentId";
	public static final String THREAD_ID_TITLE_DIVIDER = "++--++";

	private static final int THREAD_PADDING_UNIT = 15;

	private LinearLayout threadView;
	private ForumThread thread;
	private int headingPosition;
	private int threadPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thread);
		threadView = (LinearLayout) findViewById(R.id.threadsView);
		modulePosition = getIntent().getIntExtra(MainActivity.EXTRA_NAME_MODULE_POSITION, 0);
		headingPosition = getIntent().getIntExtra(ForumActivity.EXTRA_NAME_HEADING_POSITION, 0);
		threadPosition = getIntent().getIntExtra(ForumActivity.EXTRA_NAME_THREAD_POSITION, 0);
		forum = (Forum) MainActivity.modules.get(modulePosition).getElements().get(ElementType.FORUM);
		thread = forum.getHeadings().get(headingPosition).getThreads().get(threadPosition);
		setTitle(thread.getTitle());
		showThreads(thread, 0);
	}

	private void showThreads(ForumThread thread, int level) {
		LinearLayout view = new LinearLayout(this);
		view.setOrientation(LinearLayout.VERTICAL);
		view.setPadding(THREAD_PADDING_UNIT * level, 0, 0, 0);
		view.setContentDescription(thread.getId() + THREAD_ID_TITLE_DIVIDER + thread.getTitle());
		view.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			private static final String TITLE_REPLY = "Reply";

			@Override
			public void onCreateContextMenu(ContextMenu menu, final View v, ContextMenuInfo menuInfo) {
				menu.add(0, 0, 0, TITLE_REPLY).setOnMenuItemClickListener(new OnMenuItemClickListener() {
					@Override
					public boolean onMenuItemClick(MenuItem item) {
						((ThreadActivity) v.getContext()).replyThread(v.getContentDescription().toString());
						return true;
					}
				});
			}
		});
		TextView title = new TextView(this);
		title.setText(thread.toString());
		title.setLongClickable(false);
		WebView body = new WebView(this);
		body.setLongClickable(false);
		body.loadData(thread.getBody(), "text/html", null);
		view.addView(title);
		view.addView(body);
		threadView.addView(view);
		for (ForumThread r : thread.getReplies())
			showThreads(r, level + 1);
	}

	public void replyThread(String id_title) {
		Intent intent = new Intent(this, NewPostActivity.class);
		intent.putExtra(NewPostActivity.EXTRA_NAME_IS_REPLY, true);
		intent.putExtra(EXTRA_NAME_PARENT_THREAD_ID_TITLE, id_title);
		startActivityForResult(intent, NewPostActivity.REQUEST_CODE_NEW_POST);
	}

	@Override
	protected boolean handleActionRefresh() {
		threadView.removeAllViews();
		try {
			getThreads();
			thread = forum.getHeadings().get(headingPosition).getThreads().get(threadPosition);
			showThreads(thread, 0);
		} catch (Exception e) {
			Log.e("error", "Error in handleActionRefresh", e);
		}
		return true;
	}

	@Override
	protected List<?> getDisplayedItemList() {
		// do nothing
		return null;
	}

	@Override
	public Boolean goNewPost() {
		Intent intent = new Intent(getApplicationContext(), NewPostActivity.class);
		intent.putExtra(NewPostActivity.EXTRA_NAME_IS_REPLY, false);
		intent.putExtra(MainActivity.EXTRA_NAME_MODULE_POSITION, modulePosition);
		startActivityForResult(intent, NewPostActivity.REQUEST_CODE_NEW_POST);
		return true;

	}

}
